//
//  TVViewController.m
//  IPTV
//
//  Created by Echo on 13-11-19.
//  Copyright (c) 2013年 游强. All rights reserved.
//

#import "TVViewController.h"
#import "PokerControlClient.h"
#import "PokerControlClient.h"


@interface TVViewController ()

@end

@implementation TVViewController
@synthesize m_deviceInfo = _m_deviceInfo;
@synthesize m_app_cmd;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [NC addObserver:self selector:@selector(getTempInfo:) name:NOTI_TEMP_INFO object:nil];
    }
    return self;
}
-(void)dealloc{
    [[DataManager GetInstance:[PokerControlClient share].net_deviceName] releaseDataManager:[PokerControlClient share].net_deviceName];
    [NC removeObserver:self];
}

-(void)getTempInfo:(NSNotification*)noti{
    NSString *codeCmd = noti.object;
    
    if (codeCmd && m_sheet) {
        NSLog(@"codeCmd %@", codeCmd);
        if (isAddBtnPressed) {
            self.m_app_cmd = codeCmd;
            AirAddBtnViewController *studyView = [[AirAddBtnViewController alloc] init];
            studyView.delegate = self;
            [self.navigationController pushViewController:studyView animated:YES];
        }else{
            DeviceInfo *deviceInfo = [self getDeviceInfo];
            pDataInfo.isStudy = YES;
            pDataInfo.app_cmd = codeCmd;
            [pDataInfo.array removeAllObjects];
            if (deviceInfo.type==DEVICE_TV || deviceInfo.type==DEVICE_STB) {
                [self setTVBtnState:curBtn];
            }else if (deviceInfo.type == DEVICE_SOUND) {
                [self setSoundBtnState:curBtn];
            }else if (deviceInfo.type == DEVICE_AIR) {
                [self setAirBtnState:curBtn];
            }else if (deviceInfo.type == DEVICE_CUSTOM) {
                [self setCustomBtnState:curBtn];
            }
            [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
            
            if (isStudyAir) {
                [self initPicker];
            }
        }
        
        [m_sheet dismissWithClickedButtonIndex:0 animated:YES];
        m_sheet = nil;
        isAddBtnPressed = NO;
    }
}
-(void)studyDelayArray:(NSArray*)array name:(NSString*)name{
    if (array.count>5) {
        DeviceInfo *deviceInfo = [self getDeviceInfo];
        pDataInfo.isStudy = YES;
        pDataInfo.btnName = name;
        if (!pDataInfo.array) {
            pDataInfo.array = [[NSMutableArray alloc] init];
        }
        [pDataInfo.array removeAllObjects];
        [pDataInfo.array addObjectsFromArray:array];
        if (deviceInfo.type==DEVICE_TV || deviceInfo.type==DEVICE_STB) {
            [self setTVBtnState:curBtn];
        }else if (deviceInfo.type == DEVICE_SOUND) {
            [self setSoundBtnState:curBtn];
        }else if (deviceInfo.type == DEVICE_AIR) {
            [self setAirBtnState:curBtn];
        }else if (deviceInfo.type == DEVICE_CUSTOM) {
            [self setCustomBtnState:curBtn];
        }
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (modify_device_tag == actionSheet.tag) {
        if (buttonIndex == 0) {
            if ([DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count > tableview_select_index) {
                DeviceInfo *deviceInfo = [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray objectAtIndex:tableview_select_index];
                NewDevice1ViewController *view = [[NewDevice1ViewController alloc] init];
                view.type = deviceInfo.type;
                view.modify = YES;
                view.delegate = self;
                [self.navigationController pushViewController:view animated:YES];
            }
        }
        else if (buttonIndex == 1) {
            if ([DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count > tableview_select_index) {
                [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray removeObjectAtIndex:tableview_select_index];
            }
            [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
            [tableview reloadData];
            if (tableview_select_index == selDeveceIndex) {
                if (0 < [DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count) {
                    selDeveceIndex = 0;
                    [self setSelectDeviceInfoType];
                    [self initCtrlBtn];
                }
            }
            [self toBack];
        }
    }else{//组合键学习
        if (buttonIndex == 0) {//组合键学习 的单键学习
            [self getCurDeviceDataInfo:curBtn.tag-1];
        }else if (buttonIndex == 1) {//组合键学习 的多键学习
            
            DeviceInfo *deviceInfo = [self getDeviceInfo];
            if (curBtn.tag-1 < deviceInfo.keyArray.count) {
                pDataInfo = [deviceInfo.keyArray objectAtIndex:curBtn.tag-1];
            }
            StudyViewController *studyView = [[StudyViewController alloc] init];
            studyView.btnTitle = curBtn.titleLabel.text;
            studyView.delegate = self;
            [self.navigationController pushViewController:studyView animated:YES];
        }
        isMulStudy = NO;
    }
}
//调用此函数开始学习插座
-(void)getCurSocketDeviceDataInfo:(int)atag{
    NSLog(@"Socket tag %d", atag);
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    if (atag < deviceInfo.keyArray.count) {
        pDataInfo = [deviceInfo.keyArray objectAtIndex:atag];
        if (pDataInfo.isStudy) {
            if (btnView.selected) {
                [self initStudyAgain];
            }else{
                [[PokerControlClient share] startCmd:@"5" code:pDataInfo.app_cmd];
            }
        }else{
            [[PokerControlClient share] startCmd:@"9" code:nil];
            [self initStudySheet];
        }
    }
}
//调用此函数开始学习
-(void)getCurDeviceDataInfo:(int)atag{
    NSLog(@"tag %d", atag);
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    if (atag < deviceInfo.keyArray.count) {
        pDataInfo = [deviceInfo.keyArray objectAtIndex:atag];
        if (pDataInfo.isStudy) {
            if (btnView.selected) {
                if (isMulStudy) {//组合键学习的重现开始学习
                    [self startStudyKey];
                }else{
                    [self initStudyAgain];
                }
            }else{
                [[PokerControlClient share] startCmd:@"5" code:pDataInfo.app_cmd];
            }
        }else{
            [self startStudyKey];
        }
    }
}
-(void)startStudyKey{
    if (pDataInfo.airType==AIR_COLD || pDataInfo.airType==AIR_HOT || pDataInfo.airType==AIR_AUTO){
        isStudyAir = YES;
    }
    [self waitStudyCode];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == sduty_tag) {//重现学习
        if (buttonIndex == 1) {
            if (isMulStudy) {
                [self combineSheet];
            }else{
                [self startStudyKey];
            }
        }
    }else{
        isAddBtnPressed = NO;
        m_sheet = nil;
        [[PokerControlClient share] startCmd:@"0" code:nil];
    }
}
-(void)initStudyAgain{
    if ([Global curLanguageEn]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Study again" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
        alert.tag = sduty_tag;
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:STR(@"是否重新学习按键") delegate:self cancelButtonTitle:STR(@"取消") otherButtonTitles:STR(@"确定"), nil];
        alert.tag = sduty_tag;
        [alert show];
    }
}
-(void)mulStudy{
    isMulStudy = YES;
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    if (curBtn.tag-1 < deviceInfo.keyArray.count) {
        NSDataInfo *DataInfo = [deviceInfo.keyArray objectAtIndex:curBtn.tag-1];
        if (DataInfo.isStudy) {
            if (btnView.selected) {
                [self initStudyAgain];
            }else{
                if (DataInfo.array.count > 5) {
                    [[PokerControlClient share] sendArrayCmd:DataInfo.array];
                }else{
                    [[PokerControlClient share] startCmd:@"5" code:DataInfo.app_cmd];
                }
            }
        }else{
            [self combineSheet];
        }
    }
}
-(void)combineSheet{
    if ([Global curLanguageEn]){
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Prompt" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Single button to learn", @"Combine button to learn", nil];
        [sheet showInView:self.view];
    }else{
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:STR(@"提示") delegate:self cancelButtonTitle:STR(@"取消") destructiveButtonTitle:nil otherButtonTitles:STR(@"单按键学习"), STR(@"组合键学习"), nil];
        [sheet showInView:self.view];
    }
}
//音响
-(void)soundPressed:(UIButton*)abtn{
    curBtn = abtn;
    isStudyAir = NO;
    if (abtn.tag>=11 && abtn.tag<=13) {
        [self mulStudy];
    }else{
        [self getCurDeviceDataInfo:curBtn.tag-1];
    }
}
//电视
-(void)btnPressed:(UIButton*)abtn{
    curBtn = abtn;
    isStudyAir = NO;
    if (abtn.tag >=15 && abtn.tag<=20) {
        [self mulStudy];
    }else{
        [self getCurDeviceDataInfo:curBtn.tag-1];
    }
}
//空调
-(void)airBtnPressed:(GoodBtn*)abtn{
    curBtn = abtn;
    [self getCurDeviceDataInfo:curBtn.tag-1];
}
-(void)setAirBtnState:(UIButton*)btn{
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    if (btn.tag-1 < deviceInfo.keyArray.count) {
        NSDataInfo *dataInfo = [deviceInfo.keyArray objectAtIndex:btn.tag-1];
        if (dataInfo.airType == AIR_ON) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"custom_on_1.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"custom_on_2.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"custom_on_0.png") forState:UIControlStateNormal];
            }
        }else if (dataInfo.airType == AIR_OFF) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"custom_off_1.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"custom_off_2.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"custom_off_0.png") forState:UIControlStateNormal];
            }
        }else if (dataInfo.airType == AIR_COLD) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"air_cryogen_button_nor.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_cryogen_button_nor_en.png") forState:UIControlStateNormal];
                }
                [btn setBackgroundImage:IMG(@"air_cryogen_button_sel.png") forState:UIControlStateHighlighted];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_cryogen_button_sel_en.png") forState:UIControlStateHighlighted];
                }
            }else{
                [btn setBackgroundImage:IMG(@"air_cryogen_button_none.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_cryogen_button_none_en.png") forState:UIControlStateNormal];
                }
            }
        }else if (dataInfo.airType == AIR_HOT) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"air_heating_button_nor.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_heating_button_nor_en.png") forState:UIControlStateNormal];
                }
                [btn setBackgroundImage:IMG(@"air_heating_button_sel.png") forState:UIControlStateHighlighted];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_heating_button_sel_en.png") forState:UIControlStateHighlighted];
                }
            }else{
                [btn setBackgroundImage:IMG(@"air_heating_button_none.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_heating_button_none_en.png") forState:UIControlStateNormal];
                }
            }
        }else if (dataInfo.airType ==AIR_DAMP) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"air_dehumidification_button_nor.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_dehumidification_button_nor_en.png") forState:UIControlStateNormal];
                }
                [btn setBackgroundImage:IMG(@"air_dehumidification_button_sel.png") forState:UIControlStateHighlighted];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_dehumidification_button_sel_en.png") forState:UIControlStateHighlighted];
                }
            }else{
                [btn setBackgroundImage:IMG(@"air_dehumidification_button_none.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_dehumidification_button_none_en.png") forState:UIControlStateNormal];
                }
            }
        }else if (dataInfo.airType == AIR_GAS) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"air_ventilate_button_nor.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_ventilate_button_nor_en.png") forState:UIControlStateNormal];
                }
                [btn setBackgroundImage:IMG(@"air_ventilate_button_sel.png") forState:UIControlStateHighlighted];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_ventilate_button_sel_en.png") forState:UIControlStateHighlighted];
                }
            }else{
                [btn setBackgroundImage:IMG(@"air_ventilate_button_none.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_ventilate_button_none_en.png") forState:UIControlStateNormal];
                }
            }
        }else if (dataInfo.airType == AIR_AUTO) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"air_automatic_button_nor.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_automatic_button_nor_en.png") forState:UIControlStateNormal];
                }
                [btn setBackgroundImage:IMG(@"air_automatic_button_sel.png") forState:UIControlStateHighlighted];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_automatic_button_sel_en.png") forState:UIControlStateHighlighted];
                }
            }else{
                [btn setBackgroundImage:IMG(@"air_automatic_button_none.png") forState:UIControlStateNormal];
                if ([Global curLanguageEn]) {
                    [btn setBackgroundImage:IMG(@"air_automatic_button_none_en.png") forState:UIControlStateNormal];
                }
            }
        }
        GoodLabel *label = (GoodLabel *)[btn viewWithTag:100];
        if (dataInfo.airType==AIR_COLD || dataInfo.airType==AIR_HOT || dataInfo.airType==AIR_AUTO) {
            if (label) {
                if (dataInfo.isStudy) {
                    if (dataInfo.temp) {
                        label.text = dataInfo.temp;
                    }else{
                        label.text = @"16℃";
                    }
                }
            }
        }
    }
}
-(void)initMidBtn:(UIScrollView*)TVscrollview{
    
    GoodBtn *btn = [GoodBtn btnWithImg:@"ok_unstudy.png"];
    btn.tag = 11;
    btn.frame = CGRectMake((SCREEN_WIDTH-286/2)/2, 73, 286/2, 286/2);
    [TVscrollview addSubview:btn];
    if (TVscrollview.tag == 888) {//声音
        btn.tag = 7;
        [btn addTarget:self action:@selector(soundPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self setSoundBtnState:btn];
    }else{
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self setTVBtnState:btn];
    }
    
    int left_1 = 30;
    // 7, 8
    for (int i=0; i<2; i++) {
        GoodBtn *btn = [GoodBtn btnWithImg:@"left_unstudy.png"];
        btn.tag = 7+i;
        if (i == 1) {
            [btn setBackgroundImage:IMG(@"right_unstudy.png") forState:UIControlStateNormal];
        }
        if (TVscrollview.tag == 888) {
            btn.tag = 3+i;
            [btn addTarget:self action:@selector(soundPressed:) forControlEvents:UIControlEventTouchUpInside];
            [self setSoundBtnState:btn];
        }else{
            [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
            [self setTVBtnState:btn];
        }
        btn.frame = CGRectMake(left_1+86+i*82-19.5, 100, btn.frame.size.width, btn.frame.size.height);
        [TVscrollview addSubview:btn];
        
    }
    // 9, 10
    for (int i=0; i<2; i++) {
        GoodBtn *btn = [GoodBtn btnWithImg:@"up_unstudy.png"];
        btn.tag = 9+i;
        if (i == 1) {
            [btn setBackgroundImage:IMG(@"down_unstudy.png") forState:UIControlStateNormal];
        }
        if (TVscrollview.tag == 888) {
            btn.tag = 5+i;
            [btn addTarget:self action:@selector(soundPressed:) forControlEvents:UIControlEventTouchUpInside];
            [self setSoundBtnState:btn];
        }else{
            [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
            [self setTVBtnState:btn];
        }
        btn.frame = CGRectMake(left_1+100-17.5+3, 82+i*81, btn.frame.size.width, btn.frame.size.height);
        [TVscrollview addSubview:btn];
    }
}
-(void)initTitle:(NSString*)title scrollview:(UIScrollView*)scrollview{
    titleLabel = [GoodLabel labelWithFont:25 title:title];
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.text = [Global chinaToEn:title];
    titleLabel.textColor = RGB(56, 119, 221, 1);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.frame = CGRectMake(90, 20, FW(hScrollview)-90*2, 50);
    [scrollview addSubview:titleLabel];
    if (selDeveceIndex < [DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count) {
        DeviceInfo *deviceInfo = [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray objectAtIndex:selDeveceIndex];
        titleLabel.text = deviceInfo.name;
        titleLabel.text = [Global chinaToEn:deviceInfo.name];
    }
}
-(void)initTVScrollview:(int)tag{
    [self removehScrollsubView];
    UIScrollView *TVscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FH(self.view))];
    TVscrollview.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+170+70);
    if (iPhone5) {
        TVscrollview.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+170);
    }
    [hScrollview addSubview:TVscrollview];
    if (tag == 0) {
        [self initTitle:STR(@"电视") scrollview:TVscrollview];
        
    }else{
        [self initTitle:STR(@"机顶盒") scrollview:TVscrollview];
    }
    titleLabel.frame = CGRectMake(75, 20, FW(hScrollview)-75*2, 30);
    left = 20;
    // 开机 静音 1, 2 电视
    for (int i=0; i<2; i++) {
        GoodBtn *btn = [GoodBtn btnWithFont:1];
        btn.tag = 1+i;
        btn.frame = CGRectMake(left+9+i*200, 0, 127/2, 127/2);
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [TVscrollview addSubview:btn];
        [self setTVBtnState:btn];
    }
    [self initMidBtn:TVscrollview];
    //音量 3, 4 电视
    for (int j=0; j<2; j++) {
        GoodBtn *btn = [GoodBtn btnWithImg:@"add_unstudy.png"];
        btn.tag = 3+j;
        [self setTVBtnState:btn];
        btn.frame = CGRectMake(left+15, 75, btn.frame.size.width, btn.frame.size.height);
        if (j == 1) {
            btn.frame = CGRectMake(left+15, 75+124/2, 95/2, 138/2);
        }
        [TVscrollview addSubview:btn];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIImageView *imageview = [[UIImageView alloc] initWithImage:IMG(@"volume.png")];
    imageview.frame = CGRectMake(left+25, 210, 50/2, 27/2);
    [TVscrollview addSubview:imageview];
    
    //频道 5, 6 电视
    for (int j=0; j<2; j++) {
        GoodBtn *btn = [GoodBtn btnWithImg:@"nc_volume_up_unstduy.png"];
        btn.tag = 5+j;
        [self setTVBtnState:btn];
        btn.frame = CGRectMake(left+217, 75, btn.frame.size.width, btn.frame.size.height);
        if (j == 1) {
            btn.frame = CGRectMake(left+217, 75+124/2, 95/2, 138/2);
        }
        [TVscrollview addSubview:btn];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    imageview = [[UIImageView alloc] initWithImage:IMG(@"channel.png")];
    imageview.frame = CGRectMake(left+25+207, 210, 50/2, 27/2);
    [TVscrollview addSubview:imageview];
    
    // 12-14 电视
    for (int i=0; i<3; i++) {
        GoodBtn *btn = [GoodBtn btnWithImg:@"av_tv_unstudy.png"];
        btn.tag = 12+i;
        if (i == 1) {
            [btn setBackgroundImage:IMG(@"home_unstudy.png") forState:UIControlStateNormal];
        }else if (i == 2) {
            [btn setBackgroundImage:IMG(@"back_unstudy.png") forState:UIControlStateNormal];
        }
        [self setTVBtnState:btn];
        btn.frame = CGRectMake(left+59+i*60, 245, btn.frame.size.width, btn.frame.size.height);
        [TVscrollview addSubview:btn];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //自定义 15-20 电视
    for (int i=0; i<2; i++) {
        for (int j=0; j<3; j++) {
            GoodBtn *btn = [GoodBtn btnWithImg:@"custom_nor.png"];
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            btn.tag = 15+i*3+j;
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 2, 0);
            [btn setTitle:[NSString stringWithFormat:STR(@"自定义%d"), j+i*3+1] forState:UIControlStateNormal];
            if ([Global curLanguageEn]) {
                [btn setTitle:[NSString stringWithFormat:@"Custom%d", j+i*3+1] forState:UIControlStateNormal];
            }
            btn.frame = CGRectMake(left+20+j*80, 300+i*40, btn.frame.size.width, btn.frame.size.height);
            [TVscrollview addSubview:btn];
            [self setTVBtnState:btn];
            [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    //数字键 21-29 电视
    int keyleft = left-20;
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            GoodBtn *btn = [GoodBtn btnWithImg:@"key_1.png"];
            int index = i*3+j;
            btn.tag = 21+index;
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
            [btn setTitle:[NSString stringWithFormat:@"%d", index+1] forState:UIControlStateNormal];
            [self setTVBtnState:btn];
            if (index%3 == 0) {
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
            }else if (index%3 == 2) {
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 30);
            }
            if (index == 0) {
                btn.frame = CGRectMake(keyleft, 395+i*53, 115, 53);
                [btn setBackgroundImage:IMG(@"key_1.png") forState:UIControlStateNormal];
            }else if (index == 1) {
                btn.frame = CGRectMake(keyleft+115, 395+i*53, 88, 53);
                [btn setBackgroundImage:IMG(@"key_2.png") forState:UIControlStateNormal];
            }else if (index == 2){
                btn.frame = CGRectMake(keyleft+115+176/2, 395+i*53, 117, btn.frame.size.height);
                [btn setBackgroundImage:IMG(@"key_3.png") forState:UIControlStateNormal];
            }else if (index == 3){//0
                btn.frame = CGRectMake(keyleft, 395+i*53, 115, btn.frame.size.height);
                [btn setBackgroundImage:IMG(@"key_4.png") forState:UIControlStateNormal];
            }else if (index == 4){
                btn.frame = CGRectMake(keyleft+115, 395+i*53, 88, btn.frame.size.height);
                [btn setBackgroundImage:IMG(@"key_5.png") forState:UIControlStateNormal];
            }else if (index == 5){
                btn.frame = CGRectMake(keyleft+115+176/2, 395+i*53, 117, btn.frame.size.height);
                [btn setBackgroundImage:IMG(@"key_6.png") forState:UIControlStateNormal];
            }else if (index == 6){//0
                btn.frame = CGRectMake(keyleft, 395+i*53, 115, btn.frame.size.height);
                [btn setBackgroundImage:IMG(@"key_7.png") forState:UIControlStateNormal];
            }else if (index == 7){
                btn.frame = CGRectMake(keyleft+115, 395+i*53, 88, btn.frame.size.height);
                [btn setBackgroundImage:IMG(@"key_8.png") forState:UIControlStateNormal];
            }else if (index == 8){
                btn.frame = CGRectMake(keyleft+115+176/2, 395+i*53, 117, btn.frame.size.height);
                [btn setBackgroundImage:IMG(@"key_9.png") forState:UIControlStateNormal];
            }
            [TVscrollview addSubview:btn];
            [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    //数字键0 30  电视
    GoodBtn *btn = [GoodBtn btnWithImg:@"key_0.png"];
    btn.tag = 30;
    btn.frame = CGRectMake(0, 501+53, SCREEN_WIDTH, 53);
    [TVscrollview addSubview:btn];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [btn setTitle:[NSString stringWithFormat:@"%d", 0] forState:UIControlStateNormal];
    [self setTVBtnState:btn];
    [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)initAirScrollview{
    [self removehScrollsubView];
    UIScrollView *airView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [hScrollview addSubview:airView];
    
    int gag = 80;
    UIScrollView *airScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, gag, SCREEN_WIDTH, FH(airView)-64-50-gag)];
    [hScrollview addSubview:airScrollview];
    
    
    [self initTitle:STR(@"空调") scrollview:airView];
    
    NSDataInfo *dataInfo = nil;
    if ([self getDeviceInfo].keyArray.count > 0) {
        dataInfo = [[self getDeviceInfo].keyArray objectAtIndex:0];
    }
    int precount = 2;
    for (int i=0; i<precount; i++) {
        GoodBtn *btn = [GoodBtn btnWithFont:1];
        btn.tag = 1+i;
        btn.frame = CGRectMake(left+35, 15, 80/2, 80/2);
        if (i == 1) {
            btn.frame = CGRectMake(kScreenWidth-FX(btn)-FW(btn), FY(btn), FW(btn), FH(btn));
        }
        [btn addTarget:self action:@selector(airBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [airView addSubview:btn];
        [self setAirBtnState:btn];
    }
    
    int count = [self getDeviceInfo].keyArray.count-precount;
    int btn_height = 43;
    for (int i=0; i<count; i++) {
        GoodBtn *btn = [GoodBtn btnWithFont:10];
        btn.frame = CGRectMake((SCREEN_WIDTH-430/2)/2, i*(btn_height+7), 430/2, btn_height);
        btn.tag = i+1+precount;
        [btn addTarget:self action:@selector(airBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [airScrollview addSubview:btn];
        GoodLabel *label = [GoodLabel labelWithFont:14 title:@""];
        label.textAlignment = NSTextAlignmentRight;
        label.tag = 100;
        label.frame = CGRectMake(0, 0, FW(btn)-30, FH(btn)-5);
        [btn addSubview:label];
        [self setAirBtnState:btn];
    }
    airScrollview.contentSize = CGSizeMake(SCREEN_WIDTH, count*(btn_height+7));
    
    addbtn = [GoodBtn btnWithImg:@"airStencil_btn_add.png"];
    addbtn.frame = CGRectMake(SCREEN_WIDTH/2-126/4, SCREEN_HEIGHT-64-50, 126/2, 55/2);
    [addbtn addTarget:self action:@selector(addBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    [airView addSubview:addbtn];
}
-(void)editEd:(int)btntag select:(int)tempIndex{
    NSString *temp = [NSString stringWithFormat:@"%d℃", tempIndex+16];
    NSDataInfo *dataInfo = [[NSDataInfo alloc] init];
    dataInfo.isStudy = YES;
    dataInfo.temp = temp;
    dataInfo.app_cmd = self.m_app_cmd;
    if (btntag == 0) {
        dataInfo.airType  = AIR_HOT;
    }else if (btntag == 1) {
        dataInfo.airType  = AIR_COLD;
    }else if (btntag == 2) {
        dataInfo.airType  = AIR_DAMP;
    }else if (btntag == 3) {
        dataInfo.airType  = AIR_GAS;
    }else if (btntag == 4) {
        dataInfo.airType  = AIR_AUTO;
    }
    [[self getDeviceInfo].keyArray addObject:dataInfo];
    [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    [self setSelectDeviceInfoType];
    [self initCtrlBtn];
}
-(void)waitStudyCode{
    DeviceInfo *deviceInfo = [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray objectAtIndex:selDeveceIndex];
    
    if (deviceInfo.type==DEVICE_SOCKET1 || deviceInfo.type==DEVICE_SOCKET2 || deviceInfo.type==DEVICE_SOCKET3) {
        
        [[PokerControlClient share] startCmd:@"9" code:nil];
        
    }else{
        [[PokerControlClient share] startCmd:@"1" code:nil];
    }
    
    [self initStudySheet];
}
-(void)initStudySheet{
    if ([Global curLanguageEn]) {
        m_sheet = [[UIAlertView alloc] initWithTitle:@"" message:@"Wait for the learn button..." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [m_sheet show];
    }else{
        m_sheet = [[UIAlertView alloc] initWithTitle:@"" message:STR(@"等待学习按键...") delegate:self cancelButtonTitle:STR(@"取消") otherButtonTitles:nil, nil];
        [m_sheet show];
    }
}
-(void)addBtnPressed{
    [self waitStudyCode];
    isAddBtnPressed = YES;
}

-(void)initPicker{
    btnbg = [[UIButton alloc] initWithFrame:self.view.bounds];
    btnbg.backgroundColor = RGB(0, 0, 0, .4);
    [self.navigationController.view addSubview:btnbg];
    
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, FH(btnbg)-100, 0, 100)];
    if (IOS7_OR_LATER) {
        picker.backgroundColor = [UIColor whiteColor];
    }
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [btnbg addSubview:picker];
    
    CGFloat bgviewY= FY(picker)-44;
    bgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, bgviewY, SCREEN_WIDTH, 44)];
    bgview.userInteractionEnabled = YES;
    bgview.image = [UIImage imageNamed:@"popping_bg.png"];
    
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 12, SCREEN_WIDTH-50, 19)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = STR(@"请选择温度时间(单位度)");
    if ([Global curLanguageEn]) {
        label.text = @"Please choose temperature";
    }
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:14];
    label.backgroundColor = [UIColor clearColor];
    [bgview addSubview:label];
    
    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(266, 5, 53, 33)];
    [btn setTitle:STR(@"确定") forState:UIControlStateNormal];
    if ([Global curLanguageEn]) {
        [btn setTitle:@"OK" forState:UIControlStateNormal];
    }
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn addTarget:self action:@selector(pickStatureDidFinish:) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:@"popping_button.png"] forState:UIControlStateNormal];
    [bgview addSubview:btn];
    
    [btnbg addSubview:bgview];
}
-(void)pickStatureDidFinish:(UIButton*)abtn{
    GoodLabel *label = (GoodLabel *)[curBtn viewWithTag:100];
    label.text = [NSString stringWithFormat:@"%d℃", selectIndex+16];
    [btnbg removeFromSuperview]; btnbg = nil;
    if ([label.text length] > 0) {
        pDataInfo.temp = label.text;
    }else{
        pDataInfo.temp = [NSString stringWithFormat:@"%d℃", 16];
    }
    [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    if (deviceInfo.type == DEVICE_AIR) {
        [self setAirBtnState:curBtn];
    }
    isStudyAir = NO;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 15;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

#pragma UIPickerView Delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [NSString stringWithFormat:@"%d", row+16];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    selectIndex = row;
}
-(void)initTypeSelectScrollview{
    
    bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.view addSubview:bottomView];
    
    tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-100, kScreenHeight-64-50) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.backgroundColor = RGB(117, 163, 240, 1);
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [bottomView addSubview:tableview];
    
    GoodBtn *addDevice = [GoodBtn btnWithFont:16];
    addDevice.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    addDevice.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    addDevice.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [addDevice setTitle:STR(@"添加") forState:UIControlStateNormal];
    if ([Global curLanguageEn]) {
        [addDevice setTitle:@"Add" forState:UIControlStateNormal];
    }
    [addDevice setImage:IMG(@"icon_add_device.png") forState:UIControlStateNormal];
    addDevice.frame = CGRectMake(0, FH(tableview), FW(tableview), 50);
    [addDevice addTarget:self action:@selector(addDevicePressed) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:addDevice];
}
-(void)addDevicePressed{
    [self toBack];
    NewDevice1ViewController *view = [[NewDevice1ViewController alloc] init];
    view.delegate = self;
    [self.navigationController pushViewController:view animated:YES];
}
-(void)toBack{
    [UIView animateWithDuration:.3 animations:^{
        tapRecognizer.enabled = NO;
        istoLeft = NO;
        hScrollview.frame = CGRectMake(0, 0, FW(hScrollview), FH(hScrollview));
    }];
}
-(void)toLeft{
    [UIView animateWithDuration:.3 animations:^{
        tapRecognizer.enabled = YES;
        istoLeft = YES;
        hScrollview.frame = CGRectMake(FW(tableview), 0, FW(hScrollview), FH(hScrollview));
    }];
}
-(void)initCtrlBtn{
    GoodBtn *control_edit = [GoodBtn btnWithImg:@"control_edit.png"];
    control_edit.frame = CGRectMake(0, kScreenHeight/2-36/2-64, 36/2, 88/2);
    [control_edit addTarget:self action:@selector(toLeft) forControlEvents:UIControlEventTouchUpInside];
    [hScrollview addSubview:control_edit];
    hScrollview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blcustom_background_3.5.png"]];
    
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//    //imageView.backgroundColor = [UIColor redColor];
//    imageView.image = [IMG(@"background.jpg") stretchableImageWithLeftCapWidth:160 topCapHeight:460];
//    [self.view addSubview:imageView];
    
    
    [hScrollview removeGestureRecognizer:tapRecognizer];
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toBack)];
    [hScrollview addGestureRecognizer:tapRecognizer];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = STR(@"红外遥控");
    if ([Global curLanguageEn]) {
        self.title = @"Smart IR Controler";
    }
    
    [self initTypeSelectScrollview];
    
    hScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:hScrollview];
    
    [self initDeviceData];
    
    [self initCtrlBtn];
    
    if ([DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count == 0) {
        [self addDevicePressed];
    }
}
-(void)initDeviceData{
    if (!isLoadData) {
        if ([DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count > 0) {
            isLoadData = YES;
            selDeveceIndex = 0;
            [self setSelectDeviceInfoType];
        }
    }
}
//重现开始学习
-(void)reStudy:(UIButton*)studyBtn{
    btnView.selected = !btnView.selected;
}
-(void)initRightBarBtnItem{
    UIBarButtonItem *rightItem = [UIBarButtonItem backImageBarButtonItem:[UIImage imageNamed:@"btn_unstudy.png"] target:self action:@selector(reStudy:)];
    btnView = (UIButton *)[rightItem customView];
    [btnView setImage:IMG(@"btn_instudy.png") forState:UIControlStateSelected];
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)setSelectDeviceInfoType{
    self.navigationItem.rightBarButtonItem = nil;
    DeviceInfo *deviceInfo = [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray objectAtIndex:selDeveceIndex];
    if (deviceInfo.type == DEVICE_TV) {//电视
        [self initTVScrollview:0];
        [self initRightBarBtnItem];
    }else if (deviceInfo.type == DEVICE_STB) {//机顶盒
        [self initTVScrollview:1];
        [self initRightBarBtnItem];
    }else if (deviceInfo.type == DEVICE_AIR) {//空调
        [self initAirScrollview];
        [self initRightBarBtnItem];
    }else if (deviceInfo.type == DEVICE_SOUND) {//音响
        [self initSoundScrollview];
        [self initRightBarBtnItem];
    }else if (deviceInfo.type == DEVICE_CUSTOM) {//自定义
        [self initCustomScrollview];
        [self initRightBarBtnItem];
    }else if (deviceInfo.type == DEVICE_XIAOMI) {//小米盒子
        [self initXiaomiScrollview];
    }else if (deviceInfo.type == DEVICE_APPLETV) {//appletv
        [self initAppleTVScrollview];
    }else if (deviceInfo.type == DEVICE_SOCKET1) {//插座1
        [self initSocketScrollview:1];
        [self initRightBarBtnItem];
    }else if (deviceInfo.type == DEVICE_SOCKET2) {//插座2
        [self initSocketScrollview:2];
        [self initRightBarBtnItem];
    }else if (deviceInfo.type == DEVICE_SOCKET3) {//插座3
        [self initSocketScrollview:3];
        [self initRightBarBtnItem];
    }else{
        [self initRightBarBtnItem];
        [self initTVScrollview:0];//电视
    }
}
-(void)initSocketScrollview:(NSInteger)type{
    [self removehScrollsubView];
    UIScrollView *socketScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FH(self.view))];
    socketScrollview.contentSize = CGSizeMake(SCREEN_WIDTH, FH(self.view)+40);
    [hScrollview addSubview:socketScrollview];
    UIImageView *imageViewbg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    imageViewbg.image = [IMG(@"background.jpg") stretchableImageWithLeftCapWidth:160 topCapHeight:460];
    [socketScrollview addSubview:imageViewbg];
    UIImageView *imageViewSocket = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FW(socketScrollview), FH(socketScrollview))];
    imageViewSocket.userInteractionEnabled = YES;
    imageViewSocket.image = IMG(@"触摸开关地图.png");
    [socketScrollview addSubview:imageViewSocket];
    
    UIImageView *imageViewIcon = [[UIImageView alloc] initWithImage:IMG(@"f.png")];
    imageViewIcon.userInteractionEnabled = YES;
    imageViewIcon.frame = CGRectMake(FW(imageViewSocket)/2-FW(imageViewIcon)/4, 20, FW(imageViewIcon)/2, FH(imageViewIcon)/2);
    [socketScrollview addSubview:imageViewIcon];
    int t_width = 48*1.2, t_height = 45*1.2;
    if (type == 1) {
        GoodBtn *imageViewTop = [GoodBtn btnWithImg:@"d.png"];
        imageViewTop.tag = 1;
        [imageViewTop addTarget:self action:@selector(btnSocketPressed1:) forControlEvents:UIControlEventTouchUpInside];
        imageViewTop.frame = CGRectMake(FW(imageViewIcon)/2-t_width/2, FH(imageViewIcon)/2-t_width/2, t_width, t_height);
        [imageViewIcon addSubview:imageViewTop];
    }else if (type == 2) {
        for (int i=0; i<3; i++) {
            GoodBtn *btnTop = [GoodBtn btnWithImg:@"d.png"];
            btnTop.tag = 1+i;
            [btnTop addTarget:self action:@selector(btnSocketPressed2:) forControlEvents:UIControlEventTouchUpInside];
            btnTop.frame = CGRectMake(30+90*i, 35, t_width, t_height);
            if (i == 2) {
                btnTop.frame = CGRectMake(FW(imageViewIcon)/2-t_width/2, FH(imageViewIcon)/2-t_width/2+38, t_width, t_height);
                [self addBgImageview:btnTop];
            }
            [imageViewIcon addSubview:btnTop];
        }
    }else if (type == 3) {
        for (int i=0; i<4; i++) {
            GoodBtn *btnTop = [GoodBtn btnWithImg:@"d.png"];
            btnTop.tag = 1+i;
            [btnTop addTarget:self action:@selector(btnSocketPressed2:) forControlEvents:UIControlEventTouchUpInside];
            btnTop.frame = CGRectMake(30+90*i, 30, t_width, t_height);
            if (i == 1) {
                [self addBgImageview:btnTop];
            }
            else if (i == 2) {
                btnTop.frame = CGRectMake(FW(imageViewIcon)/2-t_width/2+19-8, FH(imageViewIcon)/2-t_width/2+20-8, t_width, t_height);
            }else if (i == 3) {
                btnTop.frame = CGRectMake(FW(imageViewIcon)-t_width/2-35, FH(imageViewIcon)/2-t_width/2+55, t_width, t_height);
            }
            [imageViewIcon addSubview:btnTop];
        }
    }
}
-(void)addBgImageview:(UIButton*)btn{
    UIImageView *bgtop = [[UIImageView alloc] initWithImage:IMG(@"y.png")];
    bgtop.frame = CGRectMake(-5, -5, FW(bgtop)/2, FH(bgtop)/2);
    [btn addSubview:bgtop];
}
//插座1
-(void)btnSocketPressed1:(GoodBtn*)abtn{
    curBtn = abtn;
    [self getCurSocketDeviceDataInfo:curBtn.tag-1];
}
//插座2
-(void)btnSocketPressed2:(GoodBtn*)abtn{
    curBtn = abtn;
    [self getCurSocketDeviceDataInfo:curBtn.tag-1];
}
//插座3
-(void)btnSocketPressed3:(GoodBtn*)abtn{
    curBtn = abtn;
    [self getCurSocketDeviceDataInfo:curBtn.tag-1];
}
-(void)initAppleTVScrollview{
    [self removehScrollsubView];
    UIScrollView *appletvScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FH(self.view))];
    appletvScrollview.contentSize = CGSizeMake(SCREEN_WIDTH, FH(self.view)+40);
    [hScrollview addSubview:appletvScrollview];
    
    [self initTitle:STR(@"Apple TV") scrollview:appletvScrollview];
    
    appletv = [[UIImageView alloc] initWithImage:IMG(@"apple_btn_normal.png")];
    appletv.userInteractionEnabled = YES;
    appletv.frame = CGRectMake((SCREEN_WIDTH-250)/2, 50, 250, 250);
    [appletvScrollview addSubview:appletv];
    
    GoodBtn *btn = [GoodBtn btnWithFont:1];
    btn.tag = 1;
    btn.frame = CGRectMake((FW(appletv)-150)/2, (FH(appletv)-150)/2, 150, 150);
    [btn setBackgroundImage:IMG(@"apple_middle_1.png") forState:UIControlStateNormal];
    [btn setBackgroundImage:IMG(@"apple_middle_2.png") forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(btnApplePressed:) forControlEvents:UIControlEventTouchUpInside];
    [appletv addSubview:btn];
    
    for (int i=0; i<2; i++) {
        for (int j=0; j<2; j++) {
            GoodBtn *btn = [GoodBtn btnWithFont:1];
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            btn.tag = 100+i*2+j;
            if (i*2+j==0) {
                btn.frame = CGRectMake(30, 45+40, 60, 80);
            }else if (i*2+j==1) {
                btn.frame = CGRectMake(300/2-49+60, 45+40, 50, 90);
            }else if (i*2+j==2) {
                btn.frame = CGRectMake(40+40, 30, 90, 50);
            }else if (i*2+j==3) {
                btn.frame = CGRectMake(40+40, 300/2-50+60, 90, 70);
            }
            [appletv addSubview:btn];
            [btn addTarget:self action:@selector(btnApplePressed:) forControlEvents:UIControlEventTouchDown];
            [btn addTarget:self action:@selector(btnAppleOut:) forControlEvents:UIControlEventTouchUpInside];
            [btn addTarget:self action:@selector(btnAppleOut:) forControlEvents:UIControlEventTouchDragOutside];
        }
    }
    
    for (int i=0; i<2; i++) {
        GoodBtn *btn = [GoodBtn btnWithFont:1];
        btn.tag = 2+i;
        btn.frame = CGRectMake(35+i*160, 260, 90, 90);
        if (i == 0) {
            [btn setBackgroundImage:IMG(@"apple_menu_1.png") forState:UIControlStateNormal];
            [btn setBackgroundImage:IMG(@"apple_menu_2.png") forState:UIControlStateHighlighted];
        }else{
            [btn setBackgroundImage:IMG(@"apple_pause_1.png") forState:UIControlStateNormal];
            [btn setBackgroundImage:IMG(@"apple_pause_2.png") forState:UIControlStateHighlighted];
        }
        [btn addTarget:self action:@selector(btnApplePressed:) forControlEvents:UIControlEventTouchUpInside];
        [appletvScrollview addSubview:btn];
    }
}
-(void)btnApplePressed:(GoodBtn*)apptvBtn{
    NSLog(@"btnApplePressed apptv-Btn %d ", apptvBtn.tag);
    if (apptvBtn.tag>=1 && apptvBtn.tag<=3) {
        if (apptvBtn.tag == 1) {
            [[PokerControlClient share] startCmd:@"5" code:@"0200012BAC191214421442154115171541144215411442144115411516141618141516154116401616153F1840154116161442141715161540163F163F143F164015161641160001A300012BAC151615411541173D1616183E163F173F173E183F173E1814161515171515174016161713164018141715151618141517151614411441143F163F15401715183F15000D"];
        }else if (apptvBtn.tag == 2) {
            [[PokerControlClient share] startCmd:@"5" code:@"0200012BAD1416173D1640193D16151540164016401541173F15411716141616151516183E181515401616141815171515151616161715163F15411442154115411516183F150001B800012B5714000D"];
        }else {
            [[PokerControlClient share] startCmd:@"5" code:@"0200012BA7161715411441164016151441154115411541173E173F1815171416161518153D1815173E183F154015411615173F16161616163F153F17401B3A17401516173F140001A300012CA4181515401640173F16161640173E183E183D183E183E1815151618141913173F1516141616401616161517141715181417151640173F1541144116401516173E18000D"];
        }
    }
    else if (apptvBtn.tag >= 100) {
        if (apptvBtn.tag == 100) {
            [[PokerControlClient share] startCmd:@"5" code:@"0200012BA91813183F173F193B1715193D1640163F1640183D14411615181417151715154014161A1217151640161616141813181418141341173F183E173F193C1416193E170001B800012B5119C9090001BF08000D"];
            appletv.image = IMG(@"apple_btn_left.png");
        }else if (apptvBtn.tag == 101) {
            [[PokerControlClient share] startCmd:@"5" code:@"0200012AAC17141640143F173F161615411540154115401440173F1814171416171616153F16401640163F1913171516161516171614161640173F1541154115411516183E180001AD00012B5116000D"];
            appletv.image = IMG(@"apple_btn_right.png");
        }else if (apptvBtn.tag == 102) {
            [[PokerControlClient share] startCmd:@"5" code:@"0200012BA91613173F1A3B1A3D1914163E163F1540173F183E173E1914151616161715164015411541141715411813161516161516181516401441153E1A3C174017151542140001AD00012B5318000D"];
            appletv.image = IMG(@"apple_btn_up.png");
        }else if (apptvBtn.tag == 103) {
            [[PokerControlClient share] startCmd:@"5" code:@"0200012BAC16141840183D163E1814173E173F183E183F153C1A3F1616161418131814173F163E1715173E173F16151813171618121715173F163F163E1640164016161840140001AD00012C4D19000D"];
            appletv.image = IMG(@"apple_btn_down.png");
        }
    }
}
-(void)btnAppleOut:(GoodBtn*)btn{
    NSLog(@"btnAppleOut");
    if (btn.tag >= 100) {
        appletv.image = IMG(@"apple_btn_normal.png");
    }
}
-(void)initXiaomiScrollview{
   [self removehScrollsubView];
    UIScrollView *xiaomiScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FH(self.view))];
    xiaomiScrollview.contentSize = CGSizeMake(SCREEN_WIDTH, FH(self.view)+40);
    [hScrollview addSubview:xiaomiScrollview];
   
    [self initTitle:STR(@"小米盒子") scrollview:xiaomiScrollview];
    
    
    // 开机
    GoodBtn *btn = [GoodBtn btnWithFont:1];
    btn.tag = 1;
    btn.frame = CGRectMake(20, 0, 127/2, 127/2);
    [btn setBackgroundImage:IMG(@"power_study.png") forState:UIControlStateNormal];
    [btn setBackgroundImage:IMG(@"power_sel.png") forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(btnXiaomiPressed:) forControlEvents:UIControlEventTouchUpInside];
    [xiaomiScrollview addSubview:btn];

    for (int i=0; i<2; i++) {
        GoodBtn *btn = [GoodBtn btnWithFont:1];
        btn.tag = 2+i;
        btn.frame = CGRectMake(60+i*120, 75, 154/2, 114/2);
        if (i == 0) {
            [btn setBackgroundImage:IMG(@"xiaomi_home_1.png") forState:UIControlStateNormal];
            [btn setBackgroundImage:IMG(@"xiaomi_home_2.png") forState:UIControlStateHighlighted];
        }else{
            [btn setBackgroundImage:IMG(@"xiaomi_menu_1.png") forState:UIControlStateNormal];
            [btn setBackgroundImage:IMG(@"xiaomi_menu_2.png") forState:UIControlStateHighlighted];
        }
        [btn addTarget:self action:@selector(btnXiaomiPressed:) forControlEvents:UIControlEventTouchUpInside];
        [xiaomiScrollview addSubview:btn];
    }
    
    xiaomi = [[UIImageView alloc] initWithImage:IMG(@"xiaomi_btn_normal.png")];
    xiaomi.userInteractionEnabled = YES;
    xiaomi.frame = CGRectMake((SCREEN_WIDTH-348/2)/2, 135, 348/2, 347/2);
    [xiaomiScrollview addSubview:xiaomi];
    
    for (int i=0; i<2; i++) {
        for (int j=0; j<2; j++) {
            GoodBtn *btn = [GoodBtn btnWithFont:1];
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            btn.tag = 100+i*2+j;
            if (i*2+j==0) {
                btn.frame = CGRectMake(0, 50, 58, 65);
            }else if (i*2+j==1) {
                btn.frame = CGRectMake(348/4+30, 50, 58, 65);
            }else if (i*2+j==2) {
                btn.frame = CGRectMake(45, 0, 80, 50);
            }else if (i*2+j==3) {
                btn.frame = CGRectMake(45, 347/4+30, 80, 60);
            }
            [xiaomi addSubview:btn];
            [btn addTarget:self action:@selector(btnXiaomiPressed:) forControlEvents:UIControlEventTouchDown];
            [btn addTarget:self action:@selector(btnXiaomiOut:) forControlEvents:UIControlEventTouchUpInside];
            [btn addTarget:self action:@selector(btnXiaomiOut:) forControlEvents:UIControlEventTouchDragOutside];
        }
    }
    
    btn = [GoodBtn btnWithFont:1];
    btn.tag = 4;
    btn.frame = CGRectMake((FW(xiaomi)-168/2)/2, (FH(xiaomi)-167/2)/2, 168/2, 167/2);
    [btn setBackgroundImage:IMG(@"xiaomi_middle_1.png") forState:UIControlStateNormal];
    [btn setBackgroundImage:IMG(@"xiaomi_middle_2.png") forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(btnXiaomiPressed:) forControlEvents:UIControlEventTouchUpInside];
    [xiaomi addSubview:btn];
    
    btn = [GoodBtn btnWithFont:1];
    btn.tag = 5;
    btn.frame = CGRectMake((SCREEN_WIDTH-348/2)/2, 310, 348/2, 134/2);
    [btn setBackgroundImage:IMG(@"xiaomi_back_1.png") forState:UIControlStateNormal];
    [btn setBackgroundImage:IMG(@"xiaomi_back_2.png") forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(btnXiaomiPressed:) forControlEvents:UIControlEventTouchUpInside];
    [xiaomiScrollview addSubview:btn];
}
-(void)btnXiaomiPressed:(GoodBtn*)xiaomiBtn{
    NSLog(@"xiaomi-Btn.tag %d ", xiaomiBtn.tag);
    if (xiaomiBtn.tag>=1 && xiaomiBtn.tag<=5) {
        if (xiaomiBtn.tag == 1) {
            [[PokerControlClient share] startCmd:@"5" code:@"022A151B121935183618151836181518351A14183518351A0001CD0800015F040001C00400015F040001600300015F080001BF060001C00300015F070001BF070001C0010001C00100015F0800015F0900015F0700015F0900015F0700015F0700015F0800015F0800015F090001BF0700015F0700015F080001600100015F0700015F0800015F0700015F0800015F0800015F0700015F0900015F0900015F0700015F0800015F060001600200015F07000D"];
        }else if (xiaomiBtn.tag == 2) {
            [[PokerControlClient share] startCmd:@"5" code:@"022B14192C18141921172B1B141815182D18141920192B19000157060001C00100015F0700015F080001600200015F070001BF0900015F08000D"];
        }else if (xiaomiBtn.tag == 3) {
            [[PokerControlClient share] startCmd:@"5" code:@"022B16172C19151A1F182C18151815182118151B291A281B000D"];
        }else if (xiaomiBtn.tag == 4) {
            [[PokerControlClient share] startCmd:@"5" code:@"022A15192C1A1318201B2A1A1318151A3418211913193519000D"];
        }else{
            [[PokerControlClient share] startCmd:@"5" code:@"022D14182C1915191F192C1815191319211737172C1C1D19000D"];
        }
    }
    else if (xiaomiBtn.tag >= 100) {
        if (xiaomiBtn.tag == 100) {
            [[PokerControlClient share] startCmd:@"5" code:@"022A16192A19141A1F1B2A18151815182C19351821182019000D"];
            xiaomi.image = IMG(@"xiaomi_btn_left.png");
        }else if (xiaomiBtn.tag == 101) {
            [[PokerControlClient share] startCmd:@"5" code:@"022A15192C1815181F1A2C1914181519351A1318151B2A17000D"];
            xiaomi.image = IMG(@"xiaomi_btn_right.png");
        }else if (xiaomiBtn.tag == 102) {
            [[PokerControlClient share] startCmd:@"5" code:@"02291419291C151721182C19151A1219201820192C1A341A000D"];
            xiaomi.image = IMG(@"xiaomi_btn_up.png");
        }else if (xiaomiBtn.tag == 103) {
            [[PokerControlClient share] startCmd:@"5" code:@"022915192C19141820192C191517151920182D182B191518000D"];
            xiaomi.image = IMG(@"xiaomi_btn_down.png");
        }
    }
}
-(void)btnXiaomiOut:(GoodBtn*)btn{
    if (btn.tag >= 100) {
        xiaomi.image = IMG(@"xiaomi_btn_normal.png");
    }
}
-(void)removehScrollsubView{
    for (UIView *view in [hScrollview subviews]) {
        if ([view isKindOfClass:[UIScrollView class]] || [view isKindOfClass:[GoodBtn class]]) {
            NSLog(@"xxx %@", [view class]);
            [view removeFromSuperview];
        }
    }
}
//自定义
-(void)initCustomScrollview{
    
    [self removehScrollsubView];
    
    UIScrollView *customScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FH(self.view))];
    [hScrollview addSubview:customScrollview];
    
    GoodBtn *right = [GoodBtn btnWithImg:@"custom_edite.png"];
    [right setBackgroundImage:IMG(@"custom_edit_en.png") forState:UIControlStateNormal];
    [right addTarget:self action:@selector(rightBtn) forControlEvents:UIControlEventTouchUpInside];
    right.frame = CGRectMake(SCREEN_WIDTH-50, 0, 50, 50);
    [customScrollview addSubview:right];
    
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    int tagTemp = 0;
    for (NSDataInfo *dataInfo in  deviceInfo.keyArray) {
        tagTemp++;
        GoodBtn *btn = [GoodBtn btnWithFont:12];
        btn.tag = tagTemp;
        btn.frame = CGRectMake(dataInfo.frame_x, dataInfo.frame_y, dataInfo.frame_w, dataInfo.frame_h);
        [hScrollview addSubview:btn];
        [self setCustomBtnState:btn];
        if (dataInfo.type==1 || dataInfo.type==2) {
            [btn addTarget:self action:@selector(mulCustomPressed:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            [btn addTarget:self action:@selector(customPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}
//自定义按钮被按
-(void)mulCustomPressed:(UIButton*)abtn{
    curBtn = abtn;
    isStudyAir = NO;
    [self mulStudy];
}
//自定义按钮被按
-(void)customPressed:(UIButton*)abtn{
    curBtn = abtn;
    isStudyAir = NO;
    [self getCurDeviceDataInfo:curBtn.tag-1];
}
-(void)setCustomBtnState:(UIButton*)btn{
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    if (btn.tag-1 < deviceInfo.keyArray.count) {
        NSDataInfo *dataInfo = [deviceInfo.keyArray objectAtIndex:btn.tag-1];
        NSString *str = nil, *strLignt = nil;
        if (dataInfo.isStudy) {
            str = [NSString stringWithFormat:@"%@_1", dataInfo.name];
            strLignt = [NSString stringWithFormat:@"%@_2", dataInfo.name];
        }else{
            str = [NSString stringWithFormat:@"%@_0", dataInfo.name];
        }
        [btn setBackgroundImage:IMG(str) forState:UIControlStateNormal];
        if (strLignt) {
            [btn setBackgroundImage:IMG(strLignt) forState:UIControlStateHighlighted];
        }
        if (dataInfo.type == 1) {
            [btn setTitle:@"1" forState:UIControlStateNormal];
            [self setComCustomBtn:btn dataInfo:dataInfo];//自定义
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setBackgroundImage:IMG(@"custom_button_2.png") forState:UIControlStateNormal];
        }else if (dataInfo.type == 2) {
            [btn setTitle:STR(@"自定义") forState:UIControlStateNormal];
            if ([Global curLanguageEn]) {
                [btn setTitle:@"Custom" forState:UIControlStateNormal];
            }
            [self setComCustomBtn:btn dataInfo:dataInfo];//自定义
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setBackgroundImage:IMG(@"custom_button_1.png") forState:UIControlStateNormal];
        }
    }
}
-(void)setComCustomBtn:(UIButton*)btn dataInfo:(NSDataInfo *)dataInfo{
    if (dataInfo.btnName) {
        [btn setTitle:dataInfo.btnName forState:UIControlStateNormal];
    }
    if (dataInfo.isStudy) {
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleColor:RGB(155, 220, 0, 1) forState:UIControlStateHighlighted];
    }else{
        [btn setTitleColor:RGB(8, 8, 12, 1) forState:UIControlStateNormal];
    }
}
-(void)rightBtn{
    CustomViewController *view = [[CustomViewController alloc] init];
    view.selDeveceIndex = selDeveceIndex;
    view.delegate = self;
    [self.navigationController pushViewController:view animated:YES];
}
-(void)editEd{
//    [[DataManager GetInstance] getAllDevices];
    [self initCustomScrollview];
}
-(void)initSoundScrollview{
    
    [self removehScrollsubView];
    
    UIScrollView *aoundScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FH(self.view))];
    aoundScrollview.tag = 888;
    [hScrollview addSubview:aoundScrollview];
    aoundScrollview.contentSize = CGSizeMake(SCREEN_WIDTH, FH(self.view)+50);
    
    
    [self initTitle:STR(@"音响") scrollview:aoundScrollview];
    
    
    left = 20;
    
    // 开机 静音 1, 2
    for (int i=0; i<2; i++) {
        GoodBtn *btn = [GoodBtn btnWithFont:1];
        btn.tag = 1+i;
        btn.frame = CGRectMake(left+9+i*200, 0, 127/2, 127/2);
        [self setSoundBtnState:btn];
        [btn addTarget:self action:@selector(soundPressed:) forControlEvents:UIControlEventTouchUpInside];
        [aoundScrollview addSubview:btn];
    }
    [self initMidBtn:aoundScrollview];
    // 8,9
    for (int i=0; i<2; i++) {
        GoodBtn *btn = [GoodBtn btnWithImg:@"av_tv_unstudy.png"];
        btn.tag = 8+i;
        [self setSoundBtnState:btn];
        btn.frame = CGRectMake(left+80+i*61, 235, 122/2, 103/2);
        [aoundScrollview addSubview:btn];
        [btn addTarget:self action:@selector(soundPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    //10
    GoodBtn *btn = [GoodBtn btnWithImg:@"sound_source_button_unstudy.png"];
    btn.tag = 10;
    [self setSoundBtnState:btn];
    btn.frame = CGRectMake((SCREEN_WIDTH-247/2)/2, 235+50, 247/2, 102/2);
    [aoundScrollview addSubview:btn];
    [btn addTarget:self action:@selector(soundPressed:) forControlEvents:UIControlEventTouchUpInside];
    //11-13 音响
    for (int j=0; j<3; j++) {
        GoodBtn *btn = [GoodBtn btnWithImg:@"custom_nor.png"];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        btn.tag = 11+j;
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 2, 0);
        [btn setTitle:STR(@"自定义") forState:UIControlStateNormal];
        if ([Global curLanguageEn]) {
            [btn setTitle:@"Custom" forState:UIControlStateNormal];
        }
        btn.frame = CGRectMake(left+20+j*80, 235+110, btn.frame.size.width, btn.frame.size.height+5);
        [aoundScrollview addSubview:btn];
        [self setSoundBtnState:btn];
        [btn addTarget:self action:@selector(soundPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(DeviceInfo*)getDeviceInfo{
    if (selDeveceIndex < [DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count) {
        DeviceInfo *deviceInfo = [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray objectAtIndex:selDeveceIndex];
        return deviceInfo;
    }
    return nil;
}
-(void)setSoundBtnState:(UIButton*)btn{
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    if (btn.tag-1 < deviceInfo.keyArray.count) {
        NSDataInfo *dataInfo = [deviceInfo.keyArray objectAtIndex:btn.tag-1];
        if (btn.tag == 1) {//电源键
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"power_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"power_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"power_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 2) {//静音键
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"mute_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"mute_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"mute_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 3) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"sound_left_arrow_nor.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"sound_left_arrow_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"sound_left_arrow_none.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 4) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"sound_right_arrow_nor.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"sound_right_arrow_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"sound_right_arrow_none.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 5) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"sound_up_arrow_nor.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"sound_up_arrow_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"sound_up_arrow_none.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 6) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"sound_down_arrow_nor.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"sound_down_arrow_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"sound_down_arrow_none.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 7) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"sound_center_arrow_nor.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"sound_center_arrow_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"sound_center_arrow_none.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag >= 8 && btn.tag <= 10) {
            if (btn.tag == 8) {
                if (dataInfo.isStudy) {
                    [btn setBackgroundImage:IMG(@"sound_mix_button_study.png") forState:UIControlStateNormal];
                    [btn setBackgroundImage:IMG(@"sound_mix_button_sel.png") forState:UIControlStateHighlighted];
                }else{
                    [btn setBackgroundImage:IMG(@"sound_mix_button_unstudy.png") forState:UIControlStateNormal];
                }
            }else if (btn.tag == 9) {
                if (dataInfo.isStudy) {
                    [btn setBackgroundImage:IMG(@"sound_loop_button_study.png") forState:UIControlStateNormal];
                    [btn setBackgroundImage:IMG(@"sound_loop_button_sel.png") forState:UIControlStateHighlighted];
                }else{
                    [btn setBackgroundImage:IMG(@"sound_loop_button_unstudy.png") forState:UIControlStateNormal];
                }
            }else {
                if (dataInfo.isStudy) {
                    [btn setBackgroundImage:IMG(@"sound_source_button_study.png") forState:UIControlStateNormal];
                    [btn setBackgroundImage:IMG(@"sound_source_button_sel.png") forState:UIControlStateHighlighted];
                }else{
                    [btn setBackgroundImage:IMG(@"sound_source_button_unstudy.png") forState:UIControlStateNormal];
                }
            }
        }else {
            [self setComCustomBtn:btn dataInfo:dataInfo];//音响
        }
    }
}

-(void)btnSoundPressed:(UIButton*)btn{
    [btn superview];
    NSLog(@"");
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
//允许长按菜单
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"xxx");
    tableview_select_index = indexPath.row;
    [tableview reloadData];
    UIActionSheet *sheet = nil;
    if ([Global curLanguageEn]) {
        sheet = [[UIActionSheet alloc] initWithTitle:@"Select" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Revise The Device Info", @"Delete Device", nil];
        sheet.tag = modify_device_tag;
        [sheet showInView:self.view];
    }else{
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择操作" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"修改设备信息", @"删除设备", nil];
        sheet.tag = modify_device_tag;
        [sheet showInView:self.view];
    }
    return NO;
}
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    return NO;
}
-(BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    if (action==@selector(copy:)) {
        return YES;
    }
    return NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
//        UIImageView *backgrdView = [[UIImageView alloc] initWithFrame:cell.frame];
//        backgrdView.image = IMG(@"live_list_item_normal.png");
//        cell.backgroundView = backgrdView;
    }
    
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 40)];
    CALayer *avatarLayer = [btn layer];
    [avatarLayer setMasksToBounds:YES];
    [avatarLayer setCornerRadius:20];
    
    btn.userInteractionEnabled = NO;
    [cell addSubview:btn];
    GoodLabel *nameLabel = [GoodLabel labelWithFont:13 title:@""];
    nameLabel.frame = CGRectMake(10+FW(btn)+10, 5, 240, 40);
    [cell addSubview:nameLabel];
    
    DeviceInfo *deviceInfo = nil;
    if (indexPath.row < [DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count) {
        deviceInfo = [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray objectAtIndex:indexPath.row];
    }
    if (deviceInfo) {
        if ([deviceInfo.fileid length] > 0) {
            NSString *path = [DataManager storePathWithFileName:deviceInfo.fileid];
            if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                UIImage *image = [UIImage imageWithContentsOfFile:path];
                [btn setImage:image forState:UIControlStateNormal];
            }
        }else{
            if (deviceInfo.type == DEVICE_AIR) {
                [btn setImage:IMG(@"icon_AIR.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_TV) {
                [btn setImage:IMG(@"icon_TV.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_STB) {
                [btn setImage:IMG(@"icon_STB.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_SOUND) {
                [btn setImage:IMG(@"icon_SOUND.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_CUSTOM) {
                [btn setImage:IMG(@"icon_CUSTOM.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_DISH) {
                [btn setImage:IMG(@"icon_dish.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_XIAOMI) {
                [btn setImage:IMG(@"icon_xiaomi.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_APPLETV) {
                [btn setImage:IMG(@"icon_appleTV.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_SOCKET1) {
                [btn setImage:IMG(@"icon_template_rf_one_key.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_SOCKET2) {
                [btn setImage:IMG(@"icon_template_rf_two_key.png") forState:UIControlStateNormal];
            }else if (deviceInfo.type == DEVICE_SOCKET3) {
                [btn setImage:IMG(@"icon_template_rf_three_key.png") forState:UIControlStateNormal];
            }else {
                [btn setImage:IMG(@"icon_CUSTOM.png") forState:UIControlStateNormal];
            }
        }
        nameLabel.text = deviceInfo.name;
        nameLabel.text = [Global chinaToEn:deviceInfo.name];
    }
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row < [DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count) {
        if (selDeveceIndex != indexPath.row) {
            selDeveceIndex = indexPath.row;
            [self setSelectDeviceInfoType];
            [self initCtrlBtn];
        }
        [self toBack];
    }
}

-(void)modifyNewDeviceDelegateName:(NSString*)index name:(NSString*)name fileid:(NSString *)fileid{
    NSLog(@"%@ %@", index, name);
    DeviceInfo *deviceInfo = [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray objectAtIndex:tableview_select_index];
    deviceInfo.name = name;
    deviceInfo.fileid = fileid;
    [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    [tableview reloadData];
    if (tableview_select_index != selDeveceIndex) {
        if (tableview_select_index < [DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count) {
            selDeveceIndex = tableview_select_index;
            [self setSelectDeviceInfoType];
            [self initCtrlBtn];
        }
    }
}

-(void)NewDeviceDelegateName:(NSString*)index name:(NSString*)name fileid:(NSString *)fileid{
    NSLog(@"%@ %@", index, name);
    int aselectIndex = [index intValue];
    if (aselectIndex == 0) {
//        tf.text = @"空调";
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_AIR name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }else if (aselectIndex == 1) {
//        tf.text = @"电视机";
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_TV name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }else if (aselectIndex == 2) {
//        tf.text = @"机顶盒";
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_STB name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }else if (aselectIndex == 3) {
//        tf.text = @"音响";
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_SOUND name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }else if (aselectIndex == 4) {
//        tf.text = @"自定义";
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_CUSTOM name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }
    else if (aselectIndex == 5) {
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_XIAOMI name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
//        tf.text = @"小米盒子";
    }else if (aselectIndex == 6){//Apple TV
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_APPLETV name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }else if (aselectIndex == 7){//插座1
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_SOCKET1 name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }else if (aselectIndex == 8){//插座2
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_SOCKET2 name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }else {//插座3
        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithType:DEVICE_SOCKET3 name:name fileid:fileid];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray addObject:deviceInfo];
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }
    [tableview reloadData];
    if (selDeveceIndex != ([DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count-1)) {
        selDeveceIndex = [DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count-1;
        [self setSelectDeviceInfoType];
        [self initCtrlBtn];
    }else{
        [self initDeviceData];
    }
}
//TV 一共三十个按键
-(void)setTVBtnState:(UIButton*)btn{
    DeviceInfo *deviceInfo = [self getDeviceInfo];
    if (btn.tag-1 < deviceInfo.keyArray.count) {
        NSDataInfo *dataInfo = [deviceInfo.keyArray objectAtIndex:btn.tag-1];
        if (btn.tag == 1) {//电源键
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"power_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"power_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"power_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 2) {//静音键
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"mute_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"mute_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"mute_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 3) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"add_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"add_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"add_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 4) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"reduce_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"reduce_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"reduce_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 5) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"nc_volume_up_stduy.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"nc_volume_up_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"nc_volume_up_unstduy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 6) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"nc_volume_down_stduy.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"nc_volume_down_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"nc_volume_down_unstduy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 7) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"left_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"left_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"left_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 8) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"right_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"right_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"right_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 9) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"up_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"up_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"up_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 10) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"down_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"down_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"down_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 11) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"ok_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"ok_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"ok_unstudy.png") forState:UIControlStateNormal];
            }
        }
        else if (btn.tag == 12) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"av_tv_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"av_tv_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"av_tv_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 13) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"home_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"home_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"home_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag == 14) {
            if (dataInfo.isStudy) {
                [btn setBackgroundImage:IMG(@"back_study.png") forState:UIControlStateNormal];
                [btn setBackgroundImage:IMG(@"back_sel.png") forState:UIControlStateHighlighted];
            }else{
                [btn setBackgroundImage:IMG(@"back_unstudy.png") forState:UIControlStateNormal];
            }
        }else if (btn.tag >= 15 && btn.tag <= 20) {//自定义 电视
            [self setComCustomBtn:btn dataInfo:dataInfo];
        }else if (btn.tag >= 21 && btn.tag <= 30) {
            if (dataInfo.isStudy) {
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn setTitleColor:RGB(155, 220, 0, 1) forState:UIControlStateHighlighted];
            }else{
                [btn setTitleColor:RGB(66, 66, 66, 1) forState:UIControlStateNormal];
            }
        }
    }
}


@end
